export class Tool {
  posX: number;
  posY: number;
  posZ: number;
  toolRadius: number;
  roundedTip: boolean;

  constructor(posX: number, posY: number, posZ: number, toolRadius: number, roundedTip: boolean) {
    this.posX = posX;
    this.posY = posY;
    this.posZ = posZ;
    this.toolRadius = toolRadius;
    this.roundedTip = roundedTip;
  }

  getToolTouchPoint(x: number, y: number): number {
    if (this.roundedTip) {
      const dx = x - this.posX;
      const dy = y - this.posY;
      const xyDist = Math.sqrt(dx * dx + dy * dy);
      if (xyDist > this.toolRadius) return Infinity;
      const theta = Math.acos(xyDist / this.toolRadius);
      const zDelta = Math.sin(theta) * this.toolRadius;
      return this.posZ + this.toolRadius - zDelta;
    }
    return this.posZ;
  }

  isToolTouchingPoint(x: number, y: number, z: number): boolean {
    const dx = x - this.posX;
    const dy = y - this.posY;
    const xySq = dx * dx + dy * dy;
    if (xySq > this.toolRadius * this.toolRadius) return false;
    if (!this.roundedTip) return true;
    const dz = z - (this.posZ + this.toolRadius);
    const xyzSq = xySq + dz * dz;
    return xyzSq <= this.toolRadius * this.toolRadius;
  }
}